import { createRouter, createWebHashHistory } from "vue-router"
import { getLocal } from './utils/utils'
import pinia from './store/index'
import { mainStore } from './store/main'

import Home from "./page/Home/route.js"
import Login from "./page/Login/route.js"
import My from "./page/My/route.js"
import Hot from "./page/Hot/route.js"
import Release from "./page/Release/route.js"
import  Message from "./page/Message/route"

const messageStore = mainStore(pinia)
const routes = [...Home, Login, ...My, Hot, Release,Message]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})


// 导航钩子
router.beforeEach((to, from) => {
  let token = getLocal('token')
  // 判断是否需要登录
  if(to.path != "/login" && to.meta.isLogin){
    if(!token){
      console.log("需要登录",token);
      return { path: '/login', query:{
        redirect: to.path
      }}
    }
  }
})

router.afterEach((to, from, failure) => {
  const tabListUrl = ["/", "/hot", "/release", "/message", "/my"];
  // tabbar
  if(tabListUrl.includes(to.path)){
    messageStore.tabbar = tabListUrl.indexOf(to.fullPath)
  }
})

export default router